<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 快捷登录返回
 */
class Callback extends MY_Controller {
    
    function __construct() {
        parent::__construct();
        $this->load->model('users_log_model');
        $this->load->model('users_model');
    }
    //qq登录返回
    function qq_login(){
        $qc = new QC();
        $access_token = $qc->qq_callback();
        $open_id = $qc->get_openid();
        $where = array();
        $where['open_id'] = $open_id;
        $where['access_token'] = $access_token;
        $users_bind = $this->users_model->get_qq_bind($where);
        if($users_bind){
            $uinfo = $this->users_model->get_user_by_uid($users_bind['user_id']);
            //登录成功，写入日志记录
            $log_data = array();
            $log_data['user_id'] = $uinfo['user_id'];
            $log_data['events'] = 'login';
            $log_data['content'] = 'QQ登录成功';
            $log_data['ip'] = $this->input->ip_address();
            $log_data['create_time'] = time();
            $this->users_log_model->insert($log_data);
            //更改用户信息
            $data = array();
            $data['login_times'] = $uinfo['login_times'] + 1;
            $data['last_ip'] = $uinfo['login_ip'];
            $data['last_time'] = $uinfo['login_time'];
            $data['login_ip'] = $this->input->ip_address();
            $data['login_time'] = time();
            $this->users_model->user_update($data,$uinfo['user_id']);
            //登录
            $info = array();
            $info['user_id'] = $uinfo['user_id'];
            $info['username'] = $data['username'];
            $info['mobile'] = $data['mobile'];
            $info['email'] = $data['email'];
            $info['last_ip'] = $data['last_ip'];
            $info['last_time'] = $data['last_time'];
            $this->session->set_userdata('userinfo', $info);
            redirect('user');
        }else{
            $qc1 = new QC($access_token,$open_id);
            $qc_user_info = $qc1->get_user_info();
            if($qc_user_info['ret'] == 0){
                
                $user_info = array(
                    'access_token' => $access_token,
                    'open_id' => $open_id,
                    'nickname' => $qc_user_info['nickname'],
                    'gender' => $qc_user_info['gender'] == "男" ? "1" : '2',
                    'province' => $qc_user_info['province'],
                    'city ' => $qc_user_info['city '],
                    'head_img_s' => $qc_user_info['figureurl_qq_1'],
                    'head_img_b' => $qc_user_info['figureurl_qq_2']
                );
                //已登录用户
                if($this->userinfo){
                    $bind_data = array();
                    $bind_data['user_id'] = $this->userinfo['user_id'];
                    $bind_data['access_token'] = $user_info['access_token'];
                    $bind_data['open_id'] = $user_info['open_id'];
                    $bind_data['nickname'] = $user_info['nickname'];
                    $bind_data['sex'] = $user_info['gender'];
                    $bind_data['province'] = $user_info['province'];
                    $bind_data['city'] = $user_info['city'];
                    $bind_data['head_img_s'] = $user_info['head_img_s'];
                    $bind_data['head_img_b'] = $user_info['head_img_b'];
                    $bind_data['create_time'] = time();
                    $this->users_model->insert_qq_bind($bind_data);
                    $info = array();
                    $info['nickname'] = $this->user_info['nickname'] ? $this->user_info['nickname'] : $user_info['nickname'];
                    $info['sex'] = $user_info['gender'];
                    $info['province'] = $user_info['province'];
                    $info['city'] = $user_info['province'];
                    $this->users_model->user_info_update($info,$this->userinfo['user_id']);
                    redirect('user/loginbind');exit;
                }
                
                
                $this->assign('type',1);
                $this->assign('head_img', $qc_user_info['figureurl_qq_2']);
                $this->assign('nickname', $qc_user_info['nickname']);
                $this->assign('qc_info', base64_encode(json_encode($user_info)));
            }else{
                $msg = $qc_user_info['msg'];
                $url = site_url('');
                echo "<script type='text/javascript'>alert('".$msg."');window.location.href='".$url."';</script>";
            }
            $this->assign('flag',1);
            $this->display('site/third_bind.html');
        }
        
        
    }
    //微博登陆返回
    function weibo_login(){
        $home = site_url("");
        session_start();
        $qc = new SaeTOAuthV2(WB_AKEY,WB_SKEY);
        if (isset($_REQUEST['code'])) {
            $keys = array();
            $keys['code'] = $_REQUEST['code'];
            $keys['redirect_uri'] = WB_CALLBACK_URL;
            try {
                    $token = $qc->getAccessToken( 'code', $keys ) ;
            } catch (OAuthException $e) {
            }
        }else{
            echo "<script type='text/javascript'>alert('访问超时，请重试！');window.location.href='".$home."';</script>";exit;
        }
        if($token){
            //判断是否已经绑定存在
            $where = array();
            $where['uid'] = $token['uid'];
            $where['access_token'] = $token['access_token'];
            $users_bind = $this->users_model->get_weibo_bind($where);
            if($users_bind){//已绑定
                $uinfo = $this->users_model->get_user_by_uid($users_bind['user_id']);
                //登录成功，写入日志记录
                $log_data = array();
                $log_data['user_id'] = $uinfo['user_id'];
                $log_data['events'] = 'login';
                $log_data['content'] = '微博登录成功';
                $log_data['ip'] = $this->input->ip_address();
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                //更改用户信息
                $data = array();
                $data['login_times'] = $uinfo['login_times'] + 1;
                $data['last_ip'] = $uinfo['login_ip'];
                $data['last_time'] = $uinfo['login_time'];
                $data['login_ip'] = $this->input->ip_address();
                $data['login_time'] = time();
                $this->users_model->user_update($data,$uinfo['user_id']);
                //登录
                $info = array();
                $info['user_id'] = $uinfo['user_id'];
                $info['username'] = $data['username'];
                $info['mobile'] = $data['mobile'];
                $info['email'] = $data['email'];
                $info['last_ip'] = $data['last_ip'];
                $info['last_time'] = $data['last_time'];
                $this->session->set_userdata('userinfo', $info);
                redirect('user');
            }else{//未绑定
                $qc = new SaeTClientV2(WB_AKEY,WB_SKEY,$token['access_token']);
                $uid_get = $qc->get_uid();
                $uid = $uid_get['uid'];
                $qc_user_info = $qc->show_user_by_id( $uid);//根据ID获取用户等基本信息
                if(!$qc_user_info['error_code']){
                    $locat_arr = explode(" ", $qc_user_info['location']);
                    $user_info = array(
                        'access_token' => $token['access_token'],
                        'open_id' => $token['uid'],
                        'nickname' => $qc_user_info['screen_name'],
                        'gender' => $qc_user_info['gender'] == "f" ? "2" : '2',
                        'province' => $locat_arr[0],
                        'city ' => $locat_arr[1],
                        'head_img_s' => $qc_user_info['profile_image_url'],
                        'head_img_b' => $qc_user_info['avatar_hd']
                    );
                    
                    //已登录用户
                    if($this->userinfo){
                        $bind_data = array();
                        $bind_data['user_id'] = $this->userinfo['user_id'];
                        $bind_data['access_token'] = $user_info['access_token'];
                        $bind_data['uid'] = $user_info['open_id'];
                        $bind_data['nickname'] = $user_info['nickname'];
                        $bind_data['sex'] = $user_info['gender'];
                        $bind_data['province'] = $user_info['province'];
                        $bind_data['city'] = $user_info['city'];
                        $bind_data['head_img_s'] = $user_info['head_img_s'];
                        $bind_data['head_img_b'] = $user_info['head_img_b'];
                        $bind_data['create_time'] = time();
                        
                        $this->users_model->insert_weibo_bind($bind_data);
                        $info = array();
                        $info['nickname'] = $this->user_info['nickname'] ? $this->user_info['nickname'] : $user_info['nickname'];
                        $info['sex'] = $user_info['gender'];
                        $info['province'] = $user_info['province'];
                        $info['city'] = $user_info['province'];
                        $this->users_model->user_info_update($info,$this->userinfo['user_id']);
                        redirect('user/loginbind');exit;
                    }
                    $this->assign('type',2);
                    $this->assign('head_img', $qc_user_info['avatar_hd']);
                    $this->assign('nickname', $qc_user_info['screen_name']);
                    $this->assign('qc_info', base64_encode(json_encode($user_info)));
                }else{
                    $msg = $qc_user_info['error'];
                    $url = site_url('');
                    echo "<script type='text/javascript'>alert('".$msg."');window.location.href='".$url."';</script>";
                }
                $this->assign('flag',1);
                $this->display('site/third_bind.html');
            }
            
        }else{
            echo "<script type='text/javascript'>alert('访问超时，请重试！');window.location.href='".$home."';</script>";exit;
        }
    }
    //微信快速登录
    function wx_login(){
        
    }
}
